package it.polimi.digitalstate.webbeans;

import java.util.ArrayList;

import it.polimi.digitalstate.dto.CitizenDTO;
import it.polimi.digitalstate.dto.ElectionVoteDTO;
import it.polimi.digitalstate.dto.PartyDTO;
import it.polimi.digitalstate.manager.ElectionManagerLocal;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name="electionResultBean")
@RequestScoped
public class ElectionResultBean {
	@EJB
	private ElectionManagerLocal ejb;
	private ElectionVoteDTO[] electionvote;
	
	public class PartyDetails {
		private PartyDTO party;
		private int partycount;
		
		public int getPartycount() {
			return partycount;
		}
		public void setPartycount(int partycount) {
			this.partycount = partycount;
		}
		public PartyDTO getParty() {
			return party;
		}
		public void setParty(PartyDTO party) {
			this.party = party;
		}
	}
	
	public class MemberDetails {
		private CitizenDTO member;
		private int p1_count;
		private int p2_count;
		private int p3_count;
		
		public int getP1_count() {
			return p1_count;
		}
		public void setP1_count(int p1_count) {
			this.p1_count = p1_count;
		}
		public int getP2_count() {
			return p2_count;
		}
		public void setP2_count(int p2_count) {
			this.p2_count = p2_count;
		}
		public int getP3_count() {
			return p3_count;
		}
		public void setP3_count(int p3_count) {
			this.p3_count = p3_count;
		}
		public CitizenDTO getMember() {
			return member;
		}
		public void setMember(CitizenDTO member) {
			this.member = member;
		}
	}
		
	private PartyDetails[] partydetails;
	private ArrayList<Integer> party;
	private ArrayList<Integer> partycount;
	private MemberDetails[] memberdetails;
	private ArrayList<Integer> member, p1_count, p2_count, p3_count;
	
	
	@PostConstruct
	public void init() {
		electionvote = ejb.getElectionResults();
		
		// party table logic
		party = new ArrayList<Integer>();
		partycount = new ArrayList<Integer>();
		int[] mapparty = new int[electionvote.length];
		for(int i=0;i<electionvote.length;i++)
				{
					if(!party.contains(electionvote[i].getParty().getID()))
					{
						party.add(electionvote[i].getParty().getID());
						int j = party.indexOf(electionvote[i].getParty().getID());
						mapparty[j] = i;
						partycount.add(1);
					}
					else
					{
						int j = party.indexOf(electionvote[i].getParty().getID());
						int k = partycount.get(j)+1;
					//	partycount.remove(j);
						partycount.set(j,k);
					}
				}
			partydetails = new PartyDetails[party.size()];
			for(int i=0;i<party.size();i++)
			{
				partydetails[i] = new PartyDetails();
				partydetails[i].setParty(electionvote[mapparty[i]].getParty());
				partydetails[i].setPartycount(partycount.get(i));
			}
			
		// MemberTable logic
			member = new ArrayList<Integer>();
			p1_count = new ArrayList<Integer>();
			p2_count = new ArrayList<Integer>();
			p3_count = new ArrayList<Integer>();
			int[][] mapmember = new int[electionvote.length][3];
			for(int i=0;i<electionvote.length;i++)
				for(int j=0;j<electionvote.length;j++)
					mapmember[i][j] = -2;
			for(int i=0;i<electionvote.length;i++)
					{
						if(!member.contains(electionvote[i].getP1().getID()))
						{
							member.add(electionvote[i].getP1().getID());
							int j = member.indexOf(electionvote[i].getP1().getID());
							mapmember[j][0] = i;
							p1_count.add(1);p2_count.add(0);p3_count.add(0);
						}
						else
						{
							int j = party.indexOf(electionvote[i].getP1().getID());
							int k = p1_count.get(j)+1;
						//	partycount.remove(j);
							p1_count.set(j,k);
						}			
						if (electionvote[i].getP2() != null)
						{
							if(!member.contains(electionvote[i].getP2().getID()))
							{
								member.add(electionvote[i].getP2().getID());
								int j = member.indexOf(electionvote[i].getP2().getID());
								mapmember[j][1] = i;
								p1_count.add(0);p2_count.add(1);p3_count.add(0);
							}
							else
							{
								int j = party.indexOf(electionvote[i].getP2().getID());
								int k = p2_count.get(j)+1;
							//	partycount.remove(j);
								p2_count.set(j,k);
							}				
						}
						if (electionvote[i].getP3() != null)
						{
							if(!member.contains(electionvote[i].getP3().getID()))
							{
								member.add(electionvote[i].getP3().getID());
								int j = member.indexOf(electionvote[i].getP3().getID());
								mapmember[j][2] = i;
								p1_count.add(0);p2_count.add(0);p3_count.add(1);
							}
							else
							{
								int j = party.indexOf(electionvote[i].getP3().getID());
								int k = p3_count.get(j)+1;
							//	partycount.remove(j);
								p3_count.set(j,k);
							}				
						}
					}
				memberdetails = new MemberDetails[member.size()];
				for(int i=0;i<member.size();i++)
				{
					memberdetails[i] = new MemberDetails();
					if (mapmember[i][0] != -2)	memberdetails[i].setMember(electionvote[mapmember[i][0]].getP1());
					else if (mapmember[i][1] != -2)	memberdetails[i].setMember(electionvote[mapmember[i][1]].getP2());
					else if (mapmember[i][2] != -2)	memberdetails[i].setMember(electionvote[mapmember[i][2]].getP3());
					memberdetails[i].setP1_count(p1_count.get(i));
					memberdetails[i].setP2_count(p2_count.get(i));
					memberdetails[i].setP3_count(p3_count.get(i));
				}
	
	/*		ArrayList<CitizenDTO> member = 	new ArrayList<CitizenDTO>();
			p1_count = new ArrayList<Integer>();
			p2_count = new ArrayList<Integer>();
			p3_count = new ArrayList<Integer>();
			for(int i=0;i<electionvote.length;i++)
					{
						if(!member.contains(electionvote[i].getP1()))
						{
							member.add(electionvote[i].getP1());
							int j = member.indexOf(electionvote[i].getP1());
							p1_count.add(1);p2_count.add(0);p3_count.add(0);
						}
						else
						{
							int j = party.indexOf(electionvote[i].getP1());
							int k = p1_count.get(j)+1;
						//	partycount.remove(j);
							p1_count.set(j,k);
						}			
						if (electionvote[i].getP2() != null)
						{
							if(!member.contains(electionvote[i].getP2()))
							{
								member.add(electionvote[i].getP2());
								int j = member.indexOf(electionvote[i].getP2());
								p1_count.add(0);p2_count.add(1);p3_count.add(0);
							}
							else
							{
								int j = party.indexOf(electionvote[i].getP2());
								int k = p2_count.get(j)+1;
							//	partycount.remove(j);
								p2_count.set(j,k);
							}				
						}
						if (electionvote[i].getP3() != null)
						{
							if(!member.contains(electionvote[i].getP3()))
							{
								member.add(electionvote[i].getP3());
								int j = member.indexOf(electionvote[i].getP3());
								p1_count.add(0);p2_count.add(0);p3_count.add(1);
							}
							else
							{
								int j = party.indexOf(electionvote[i].getP3());
								int k = p3_count.get(j)+1;
							//	partycount.remove(j);
								p3_count.set(j,k);
							}				
						}
					}
				memberdetails = new MemberDetails[member.size()];
				for(int i=0;i<member.size();i++)
				{
					memberdetails[i] = new MemberDetails();
					memberdetails[i].setMember(member.get(i));
					memberdetails[i].setP1_count(p1_count.get(i));
					memberdetails[i].setP2_count(p1_count.get(i));
					memberdetails[i].setP3_count(p1_count.get(i));
				}
		*/ 
	}
	
	public PartyDetails[] getPartydetails (){
		return partydetails;
	}
	
	public void setPartydetails(PartyDetails[] partydetails){
		this.partydetails = partydetails;
	}
	
	public MemberDetails[] getMemberdetails() {
		return this.memberdetails;
	}
	public void setMemberDetails(MemberDetails[] memberdetails) {
		this.memberdetails = memberdetails;
	}
	public ArrayList<Integer> getParty() {
		return party;
	}
	public void setParty(ArrayList<Integer> party) {
		this.party = party;
	}
	public ArrayList<Integer> getPartycount (){
		return partycount;
	}
	public void setPartycount(ArrayList<Integer> partycount){
		this.partycount = partycount;
	}
}
